<?php
namespace app\api\controller\user;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\buss\Buss as BussModel;
use app\common\model\orderdata\Goods;
use app\api\controller\Base;
use app\common\model\power\Rolepower;
class Buss extends Base
{
	
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(201004);
	}

	/**
	 * 获取企业信息
	 * @return [type] [description]
	 */
	public function getbussinfo(){
		$company = BussModel::user_getinfo('',true);
		$wz = mb_strpos($company['UserName'],'(未备案)');
		if($wz!==false){
			$company['UserName'] = mb_substr($company['UserName'],0,$wz);
		}
		// $userdata = UserModel::getinfo();
		// //为空则给企业返回修改信息
		// if(empty($company['UserTel'])){
		// 	$company['UserTel'] = $userdata['Iphone']; 
		// }
		// //为空则给企业返回修改信息
		// if(empty($company['UserName'])){
		// 	$company['UserName'] = $userdata['Name']; 
		// }
		return myok($company);
		
	}

	public function getgoodlist(){
		$goodlist = Goods::getlist();
		return myok($goodlist);
	}
	/**
	 * 修改/完善企业信息 
	 * 产废人信息也再次完善
	 * @return [type] [description]
	 */
	public function rebussinfo(){
		$data = input('post.');
		$company = BussModel::user_getinfo();
		//为空则给企业返回修改信息
		if(empty($data)){
			return myok($company);
		}
		if(empty($company)){
			//创建一个企业信息
			$result = $this->validate($data,'app\common\validate\Bussinfo');
        	if (true !== $result) {
			    return myerr($result);
			}
			$company = BussModel::user_create($data);
			return myok('备案信息已完善，请等待审核');
		}else{
			//修改企业信息
			$sb = BussModel::user_reinfo($data);
			return myok('备案信息已修改，请等待审核');
		}
	}

	/**
	 * 图片上传
	 * @return [type] [description]
	 */
	public function upimg(){
		$type = empty(input('type'))?'other':input('type');
		if(!$type=='company'||!$type=='user'||!$type=='order'){
			$type = 'other';
		}
		try {
			$file = request()->file('img');
		} catch (\Exception $e) {
			return json_encode(['status'=>'error','data'=>$e->getMessage()]);
		}
        // 移动到框架应用根目录/public/uploads/ 目录下
        if(!is_null($file)){
           	try{
           		$save_path = Env::get('root_path').'public\\static' .'\\upimg\\buss\\'.$type.'\\';
	            $info = $file->validate(["size"=>50000000,'ext'=>'jpg,png,gif'])->rule("date")->move($save_path);
	            //echo $save_path. DS . date("Ymd") . DS .  $info->getFilename();
	            $image = \think\Image::open($save_path . '\\' . $info->getSaveName());  
	            $image->thumb(2000, 2000)->save($save_path .  '\\' . $info->getSaveName());  
	            if($info){//getSaveName();
	                $p=$info->getFilename();
	                return json_encode(['status'=>'ok','data'=>SERVERURL.'static/upimg/buss/'.$type.'/'.date('Ymd',time()).'/'.$p]);
	            }else{
	              return json_encode(['status'=>'error','data'=>$info->getError()]);
	            }
           	} catch (\Exception $e) {
				return json_encode(['status'=>'error','data'=>$e->getMessage()]);
			}	
            
         }else{
              return json_encode(['status'=>'error','data'=>'没有找到需要上传的图片']);
         }
	}


	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
}
